package projects;

import getData.GetDataFromProjectXML;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class WriteGroupsToXMLwithProjects extends GetDataFromProjectXML {

	@SuppressWarnings("static-access")
	public static void updateProjectListXML(List<String> listOfProjects, String listOfProjectsFileName) {
	GetDataFromProjectXML getGroupsFromXML = new GetDataFromProjectXML();		
		try {
			Properties properties = new Properties();
					for (String item:listOfProjects) {
						
						listOfFrames =  new ArrayList<String>();	
						listOfFrames = getGroupsFromXML.getOldListOfFramesInCurrentProject(item, listOfProjectsFileName);
					//	System.out.println("listofframes" + listOfFrames);
						String[] strarray = listOfFrames.toArray(new String[0]);
						properties.setProperty(item, Arrays.toString(strarray));
					}			
			File file = new File(listOfProjectsFileName);
			FileOutputStream fileOut = new FileOutputStream(file);
			properties.storeToXML(fileOut, "List of projects");
			fileOut.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public WriteGroupsToXMLwithProjects() {
		super();
	}


	@SuppressWarnings("static-access")
	public static void addListOfFramesToProjectAtXML(List<String> listOfProjects,
			List<String> listOfFrames, String listOfProjectsFileName,
			String nameOfCurrentProject, String[] listOfFramesForCurrentProject) {
		GetDataFromProjectXML getGroupsFromXML = new GetDataFromProjectXML();
		  try {
				Properties properties = new Properties();
						for (String item:listOfProjects) {
							//System.out.println("item " + item);
							listOfFrames =  new ArrayList<String>();	
							listOfFrames = getGroupsFromXML.getOldListOfFramesInCurrentProject(item, listOfProjectsFileName);
							//	System.out.println("listofframes" + listOfFrames);
							String[] strarray = listOfFrames.toArray(new String[0]);
							
							if (item != nameOfCurrentProject){
								//System.out.println("strarray " + strarray);
								properties.setProperty(item, Arrays.toString(strarray));
								}
								else  {
								properties.setProperty(item, Arrays.toString(listOfFramesForCurrentProject));
								//System.out.println("listOfFramesForEditedProject " + listOfFramesForEditedProject);
								}
							}
					
				File file = new File(listOfProjectsFileName);
				FileOutputStream fileOut = new FileOutputStream(file);
				properties.storeToXML(fileOut, "List of projects");
				fileOut.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}	
	}
}